package fr.inserm.u915.tabixserver;

import java.util.ArrayList;
import java.util.List;


public abstract class ListOfInstances extends Named
	{
	private List<Instance> instances=new ArrayList<Instance>();
	private List<Section> subList=new ArrayList<Section>(1);
	protected ListOfInstances()
		{
		
		}
	
	public List<Instance> getAllInstances()
		{
		List<Instance> all=new ArrayList<Instance>();
		collectInstances(this,all);
		return all;
		}
	
	
	public Instance getDeepInstanceById(String id)
		{
		for(Instance i:this.instances)
			{
			if(i.getId().equals(id)) return i;
			}
		for(Section child:subList)
			{
			Instance i=child.getDeepInstanceById(id);
			if(i!=null) return i;
			}
		return null;
		}
	
	private static void collectInstances(ListOfInstances curr,List<Instance> all)
		{
		all.addAll(curr.getInstances());
		for(Section section:curr.getSections())
			{
			collectInstances(section,all);
			}
		}
		
	public List<Instance> getInstances()
		{
		return this.instances;
		}
	public List<Section> getSections()
		{
		return this.subList;
		}
	
	void addSection(Section s)
		{
		this.subList.add(s);
		}
	void addInstance(Instance i)
		{
		this.instances.add(i);
		}

	}
